package com.liuyang.homework.Bean;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.liuyang.homework.SQL.NewsBaseHelper;
import com.liuyang.homework.SQL.NewsCursorWrapper;
import com.liuyang.homework.SQL.NewsDbSchema;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * Created by liuyang on 2017/1/14.
 */

public class NewsLab {

    private static NewsLab newsLab;

    private static Context context;

    private SQLiteDatabase sqLiteDatabase;

    public NewsLab(Context context) {
        this.context = context.getApplicationContext();
        this.sqLiteDatabase = new NewsBaseHelper(this.context).getWritableDatabase();
    }

    public static NewsLab get(Context context){
        if (newsLab == null){
            newsLab = new NewsLab(context);
        }
        return newsLab;
    }

    private static ContentValues getContentValues(News news){
        ContentValues values = new ContentValues();

        values.put(NewsDbSchema.NewsTable.Cols.UUID,news.getUuid().toString());
        values.put(NewsDbSchema.NewsTable.Cols.TITLE, news.getTitle());
        values.put(NewsDbSchema.NewsTable.Cols.CONTENT,news.getContent());
        values.put(NewsDbSchema.NewsTable.Cols.ISKEY, news.isKey()?1:0);
        values.put(NewsDbSchema.NewsTable.Cols.KEYWORDS,news.getKeyWords());
        values.put(NewsDbSchema.NewsTable.Cols.CREATEDAT,news.getCreatedAt());
        values.put(NewsDbSchema.NewsTable.Cols.IMAGE,news.getImage());
        values.put(NewsDbSchema.NewsTable.Cols.SOURCE,news.getSource());
        values.put(NewsDbSchema.NewsTable.Cols.URL,news.getUrl());

        return values;
    }

    private NewsCursorWrapper queryNews(String whereClause,String[] whereArgs){

        Cursor cursor = sqLiteDatabase.query(
                NewsDbSchema.NewsTable.NAME,
                null,
                whereClause,
                whereArgs,
                null,
                null,
                null);

        return new NewsCursorWrapper(cursor);

    }

    public void addNews(News news){
        ContentValues values = getContentValues(news);
        sqLiteDatabase.insert(NewsDbSchema.NewsTable.NAME,null,values);
    }

    public void deleteNews(News news){
        String uuidString = news.getUuid().toString();
        sqLiteDatabase.delete(NewsDbSchema.NewsTable.NAME, NewsDbSchema.NewsTable.Cols.UUID+" =? ",
                new String[]{uuidString});
    }

    public void updateNews(News news){
        String uuidString = news.getUuid().toString();
        ContentValues values = getContentValues(news);
        sqLiteDatabase.update(NewsDbSchema.NewsTable.NAME,values, NewsDbSchema.NewsTable.Cols.UUID+" =? ",
                new String[]{uuidString});
    }

    public News getNews(UUID uuid){
        NewsCursorWrapper cursor = queryNews(NewsDbSchema.NewsTable.Cols.UUID+" = ?",new String[]{uuid.toString()});
        try {
            if (cursor.getCount() == 0){
                return null;
            }
            return cursor.getNews();
        }finally {
            cursor.close();
        }
    }

    public List<News> getNews(){
        List<News> news = new ArrayList<>();
        NewsCursorWrapper cursor = queryNews(null,null);
        try {
            cursor.moveToFirst();
            while (!cursor.isAfterLast()){
                news.add(cursor.getNews());
                cursor.moveToNext();
            }
        }finally {
            cursor.close();
        }
        return news;
    }


}
